/*
 * dijkstra_vector.c
 *
 *  Created on: Jun 18, 2010
 *      Author: Guilherme
 */

#include "dijkstra_vector.h"
#include "../graphgenerator/dgraph.h"
#include <time.h>

//char *file_name = "logs.txt";
//FILE *file;
time_t begin, end, elapsed;

void dijkstra_vector(AdjacencyList graph, LinkedList source, double prob) {
	begin = clock();
	vector_t *v;
	vector_element_t *e;
	LinkedList neighbor;
	int i;
	int dist[graph->numberNodes + 1];

	v = init_vector(graph->numberNodes);
	for (i = 1; i <= graph->numberNodes; i++) {
		dist[i] = INT_MAX;
		if ((e = malloc(sizeof(vector_element_t))) == NULL) {
			fprintf(stderr, "Could not allocate memory for a new vector element");
			exit(2);
		}
		e->nodeID = i;
		e->distance = INT_MAX;
		insert_element(v, e);
	}

	dist[source->nodeID] = 0;
	decrease_key(v, source->nodeID, 0);
	while (!is_empty(v)) {
		e = delete_min(v);
		while ((neighbor = getNeighbor(graph, e->nodeID)) != NULL) {
			distance d = dist[e->nodeID] + neighbor->linkCost;
			if (d < dist[neighbor->nodeID]) {
				dist[neighbor->nodeID] = d;
				decrease_key(v, neighbor->nodeID, d);
			}
			free(neighbor);
		}
	}
	end = clock();
	elapsed = end - begin;

	//fprintf(file, "%s\t%d\t%.2f\t%f\n", "VECTOR", graph->numberNodes, prob, (double)elapsed/CLOCKS_PER_SEC);
	fprintf(stdout, "%s\t%d\t%.2f\t%f\n", "VECTOR", graph->numberNodes, prob, (double)elapsed/CLOCKS_PER_SEC);

	destroy_vector(v);

}

/*int main(int argc, char **argv) {
	AdjacencyList L;
	dgraph_t *graph;
	dgraph_edge_t *edge_ptr;
	int i, nodes, replications;
	double prob;

	file = fopen(file_name, "a+");

	for (nodes = 100; nodes <= 500; nodes += 100) {
		for (prob = 0.1; prob <= 1.0; prob += .3) {
			for (replications = 1; replications <= 30; replications++) {
				L = initAdjacencyList(nodes);

				graph = dgraph_rnd_dense(nodes, prob);

				for (i = 0; i < graph->n; i++) {
					edge_ptr = graph->vertices[i].first_edge;
					while (edge_ptr) {
						L = addNeighbor(L, i + 1, edge_ptr->vertex_no + 1, edge_ptr->dist);
						edge_ptr = edge_ptr->next;
					}
				}
				dgraph_free(graph);
				dijkstra_vector(L, L->nodes[1], prob);
				destroy_adjacency_list(L);
			}
		}
	}

	fclose(file);

	return 0;
}*/
